/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Initial Developer of the Original Code is
*   Nick Kreeger
* Portions created by the Initial Developer are Copyright (C) 2006-2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*   Nick Kreeger <nick.kreeger@park.edu>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */

#ifndef __CHMsgService_h_
#define __CHMsgService_h_

#import <AppKit/AppKit.h>

#import "nsIMsgDBView.h"
#import "nsIMessenger.h"
#import "nsIMsgWindow.h"
#import "nsIMsgAccountManager.h"
#import "nsIMsgMailSession.h"
#import "nsIMsgComposeService.h"
#import "nsIMsgFolder.h"
#import "nsIFolderListener.h"
#import "nsIRDFCompositeDataSource.h"
#import "nsIMsgRDFDataSource.h"
#import "nsIRDFService.h"
#import "nsCOMPtr.h"
#import "nsIFolderListener.h"
#import "nsIMsgStatusFeedback.h"
#import "nsISupportsArray.h"

@class CHBrowserView;
@class DummyMessageHeader;
@class MessageWindowService;

class nsIMsgDBHdr;
class nsIMsgMailNewsUrl;
class CHMsgDBViewWrapper;
class CHUrlListener;

/**
 * This class provides a layer of interaction
 * with the gecko mailnews components and provides
 * feedback to |MessageWindowService|
 */
class CHMsgService : public nsIMsgStatusFeedback,
                     public nsIFolderListener
{
public:
  CHMsgService();
  virtual ~CHMsgService();
  
  NS_DECL_ISUPPORTS
  NS_DECL_NSIFOLDERLISTENER
  NS_DECL_NSIMSGSTATUSFEEDBACK
    
  /**
   * Setup the mail service, initializes mailnews gecko services.
   * @param aMessageWindowService The delegate to notify of gecko events.
   */
  NS_IMETHOD Init(MessageWindowService* aMessageWindowService);
  
  /**
   * Setup the embedded view with the mailnews services. The initialized
   * message window will become the current message window.
   * @param inBrowserView The browser view to setup.
   */
  NS_IMETHOD InitNewMsgWindow(CHBrowserView* inBrowserView);
  
  /**
   * Set the current 'focused' message window by passing a browser view
   * that has already been initialized by |InitNewMsgWindow()|. Calling 
   * this will make all gecko mailnews events display or spawn off of the 
   * parent container of the |CHBrowserView|.
   *
   * If a match is not found, the current message window will be set to |null|.
   *
   * @param aBrowserView The browser view to set as the active view.
   */
  NS_IMETHOD SetCurrentMsgWindow(CHBrowserView* aBrowserView);
  
  /**
   * Remove a message window from the stored list. Call this function
   * before the containers parent is released.
   * @param aBrowserView The browser view that will be closed.
   */
  NS_IMETHOD RemoveMsgWindow(CHBrowserView* aBrowserView);
  
  NS_IMETHOD Shutdown();
  
  /**
   * Retrieve new messages for a message folder.
   * @param inMsgFolder The message folder to download messages to.
   */
  NS_IMETHOD GetNewMessages(nsIMsgFolder* inMsgFolder);
  
  /**
   * - 0.1: These functions do not work.
   */
  NS_IMETHOD GetNewMessagesForAllServers();
  NS_IMETHOD CreateNewFolder();
  NS_IMETHOD CreateNewSubfolder();
  
  /**
   * Load a message by a message's URI. Calling this function
   * multiple times can cause a thread lock, especially with IMAP
   * URI's, so use care when calling this function.
   * @param aUriSpec The message URI to open.
   */
  NS_IMETHOD OpenUri(const char* aUriSpec);
  NS_IMETHOD StopUrls();
  
  NS_IMETHOD OnMsgLoaded(nsIMsgMailNewsUrl* aUrl, DummyMessageHeader* aDummyMessageHeader);
  NS_IMETHOD OnEndAttachments(NSArray* inAttachments);
  
  NS_IMETHOD MsgHdrFromURI(const char* aURI, nsIMsgDBHdr** aMsgHdr);  // should be private?
  NS_IMETHOD GetMessenger(nsIMessenger** aRetVal);
  
  /**
   * Open an attachment with the given params.
   * 1/16/2006 - Not sure if this prompts something...
   */
  NS_IMETHOD OpenAttachment(const char* aContentType, 
                            const char* aUrl, 
                            const char* aDisplayName, 
                            const char* aMessageUri, 
                            PRBool isExternalAttachment);
  
protected:
  void OnStartRunningUrl();
  void OnStopRunningUrl();
  
  /**
   * This function handles all MDN response generations (i.e. imap, pop).
   * For pop, the msg UID can be 0 (i.e. 1st message in a local folder) so
   * no need to check the UID here. No one seems to set mimeHeaders to null
   * so no need to check it either.
   * @param aUrl - The message URL to validate with MDN.
   *
   * XXX this function should be somewhere else.
   */
  NS_IMETHOD HandleMDNResponse(nsIMsgMailNewsUrl* aUrl);
    
private:
  nsCOMPtr<nsIMessenger>     mMessenger;
  nsCOMPtr<nsIMsgWindow>     mCurMsgWindow;  // fyi, these are conviently kept track of in |nsIMsgMailSesssion|.
  nsCOMPtr<nsISupportsArray> mMsgWindows;
  nsXPIDLCString             mCurMessageUri;
  PRBool                     mIsRunningUri;
  PRInt32                    mCurrentBiffState;
  
  MessageWindowService*      mMessageWindowService;  // weak
  
  friend class CHUrlListener;
};

#endif
